package soa.security.web.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import soa.security.Constants;
import soa.security.domain.Account;
import soa.security.domain.Department;
import soa.security.domain.Group;
import soa.security.domain.LdapTree;
import soa.security.domain.Organization;
import soa.security.domain.Person;
import soa.security.domain.Position;
import soa.security.web.export.LdapTreeJsonFormatExport;
import soa.security.web.support.action.LdapModelAction;
import v1.web.WebUtils;

/**
 * 列表的例子，新增的例子，看方法grid，add,save，
 * 
 * 
 * @author hueb
 *
 *         2015年4月22日
 */
public class PersonAction extends LdapModelAction<Person> {

	private static final long serialVersionUID = 6145782274740130027L;

	private String personUid;
	private String accountDns;
	private String organizationOrigId;
	private String departmentOrigId;
	private String deptOrigName;

	private String positionDns;
	private String groupDns;
	private boolean official;

	public String grid() {
		if (StringUtils.isBlank(organizationId))
			return SUCCESS;

		if (StringUtils.isNotBlank(departmentId)) {
			
//			updateResultContent(departmentMgrService.allPersons(organizationId,
//					departmentId, this.page.getRequest()));
			
			List<Person> all = personMgrService.allPersons(organizationId,
					buildContext().getParameters(), subTree);
			
			updateResultContent(personMgrService.allPersons(all,
					departmentMgrService.allPersons(organizationId,
							departmentId), this.page.getRequest()));
			
			
		} else
			updateResultContent(personMgrService.allPersons(organizationId,
					buildContext().getParameters(), subTree, this.page.getRequest()));

		return SUCCESS;
	}

	public String edit() {
		Person person = getModel();
		if (StringUtils.isBlank(person.getUid()))
			person.setUid("PE" + personMgrService.findMaxNumber(Person.class));

		updateResultContent(wrap(person));

		orId = person.getOrganization().getBid();
		return SUCCESS;
	}

	public String view() {
		Person person = getModel();
		Organization or = person.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);
		person.setOrganization(or);

		orId = person.getOrganization().getBid();
		return SUCCESS;
	}

	public String doneSave() {
		Person per = getModel();
		per.setOfficial(isOfficial() ? 1 : 0);
		personMgrService.addPerson4Mgr(organizationId, per, departmentDns,
				positionDns, groupDns);

		/*
		 * personMgrService.addPerson(organizationId, getModel()); if
		 * (StringUtils.isNotBlank(departmentId))
		 * departmentMgrService.addPersons(organizationId, departmentId,
		 * getModel().getUid());
		 */

		return SUCCESS;
	}

	public String doneUpdate() {
		Person per = getModel();
		per.setOfficial(isOfficial() ? 1 : 0);
		personMgrService.updatePerson4Mgr(organizationId, per, departmentDns,
				positionDns, groupDns);
		// personMgrService.updatePerson(organizationId, getModel());
		return SUCCESS;
	}

	public String accounts() {
		Person person = getModel();
		Organization or = person.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);
		person.setOrganization(or);

		orId = person.getOrganization().getBid();
		return SUCCESS;
	}
	
	public String accountGrid() {
		List<Account> list = accountMgrService.findByPersonUid(organizationId,
				personUid);
		updateResultContent(list);
		return SUCCESS;
	}

	public String nonaccounts() {
		List<Account> list = accountMgrService.findNonByPersonUid(
				organizationId, getModel().getUid());
		updateResultContent(list);

		return SUCCESS;
	}

	public String addAccount() {
		return SUCCESS;
	}

	public String doneSaveAccount() {
		accountMgrService.updateAccount(organizationId, accountDns, personUid);

		return SUCCESS;
	}

	public String personIn() {
		return SUCCESS;
	}

	/**
	 * 人员调入页面的树，指定机构下的部门树，过滤指定部门
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */

	public String gridIn() {
		List<Person> filters = new ArrayList<Person>();
		// 部门调入
		if (StringUtils.isNotBlank(departmentOrigId)) {
			filters = departmentMgrService.allPersons(organizationOrigId,
					departmentOrigId);

			// 部门点击，显示部门下的人员，过滤原来的人
			if (StringUtils.isNotBlank(departmentId))
				updateResultContent(personMgrService.filterPersons(
						departmentMgrService.allPersons(organizationId,
								departmentId), filters, organizationId));
			// 机构点击，显示只在机构下的人
			else
				updateResultContent(personMgrService.filterPersons(
						personMgrService.allPersons(organizationId), filters,
						organizationId));
			// 机构调入,不支持
		} else {
			// if (StringUtils.isNotBlank(departmentId))
			// updateResultContent((departmentMgrService.allPersons(
			// organizationId, departmentId)));
		}

		return SUCCESS;
	}

	/**
	 * 指定机构下的部门树，需要checked
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */
	public String deptInOneOrgCheckTree() {
		List<Department> checked = personMgrService.findDepartments(
				organizationId, Constants.PERSONS, dnp);
		List<LdapTree> trees = treeMgrService.deptInOneOrgCheckTree(
				organizationId, checked, departmentId);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	/**
	 * 指定机构下的用户组树，需要checked
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年5月29日
	 */
	public String groupInOneOrgCheckTree() {
		List<Group> checked = personMgrService.findGroups(organizationId, dnp);
		List<LdapTree> trees = treeMgrService.groupInOneOrgCheckTree(
				organizationId, checked);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	/**
	 * 指定机构下的岗位树，需要checked
	 * 
	 * @return String
	 * 
	 * @author Cre.Gu at 2015年6月2日
	 */
	public String positionInOneOrgCheckTree() {
		List<Position> checked = personMgrService.findPositions(organizationId,
				dnp);
		List<LdapTree> trees = treeMgrService.positionInOneOrgCheckTree(
				organizationId, checked);

		LdapTreeJsonFormatExport content = new LdapTreeJsonFormatExport(trees);
		return WebUtils.writeJson(ServletActionContext.getResponse(),
				content.toString());
	}

	@Override
	public String remove() {
		personMgrService.remove(organizationId, dnp);
		return SUCCESS;
	}

	@Override
	protected Person findByDn(String dn) {
		return personMgrService.findByDn(dn);
	}

	protected Person wrap(Person person) {
		if (person == null)
			return person;

		Organization or = person.getOrganization();
		if (or == null)
			or = getOrganization(organizationId);

		person.setOrganization(or);
		return person;
	}

	public String getPersonUid() {
		return personUid;
	}

	public void setPersonUid(String personUid) {
		this.personUid = personUid;
	}

	public String getAccountDns() {
		return accountDns;
	}

	public void setAccountDns(String accountDns) {
		this.accountDns = accountDns;
	}

	public String getOrganizationOrigId() {
		return organizationOrigId;
	}

	public void setOrganizationOrigId(String organizationOrigId) {
		this.organizationOrigId = organizationOrigId;
	}

	public String getDepartmentOrigId() {
		return departmentOrigId;
	}

	public void setDepartmentOrigId(String departmentOrigId) {
		this.departmentOrigId = departmentOrigId;
	}

	public String getDeptOrigName() {
		return deptOrigName;
	}

	public void setDeptOrigName(String deptOrigName) {
		this.deptOrigName = deptOrigName;
	}

	public String getPositionDns() {
		return positionDns;
	}

	public void setPositionDns(String positionDns) {
		this.positionDns = positionDns;
	}

	public String getGroupDns() {
		return groupDns;
	}

	public void setGroupDns(String groupDns) {
		this.groupDns = groupDns;
	}

	public boolean isOfficial() {
		return official;
	}

	public void setOfficial(boolean official) {
		this.official = official;
	}
}
